﻿using QuanLyGiaoVuDAL.ADOWrapper;
using QuanLyGiaoVuDAL.Mapper;
using QuanLyGiaoVuDAL.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyGiaoVuDAL.Repositories.DatabaseRepositories
{
    public class ThongBaoRepository:IThongBaoRepository
    {

        public ThongBaoDTO InsertThongBao(ThongBaoDTO thongBaoDTO)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoInsert]", GetParameterArray(thongBaoDTO));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToThongBaoDTO();
            }
            catch (Exception ex)
            {
                throw new Exception("Gửi thông báo không thành công.");
            }
        }
        public ThongBaoDTO GetThongBaoById(Guid MaThongBao)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoSelect]", DBParameterFactory.Create("@MaThongBao", MaThongBao));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToThongBaoDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public IEnumerable<ThongBaoDTO> GetAllThongBao(Guid? maNguoiGui)
        {
            List<ThongBaoDTO> listThongBao = new List<ThongBaoDTO>();
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = DBParameterFactory.Create("MaThongBao", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[1] = maNguoiGui.HasValue ? DBParameterFactory.Create("NguoiGuiId", SqlDbType.UniqueIdentifier, maNguoiGui) : DBParameterFactory.Create("NguoiGuiId", SqlDbType.UniqueIdentifier, DBNull.Value);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoSelect]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listThongBao.Add(dataRow.ToThongBaoDTO());
                }
                return listThongBao.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool DeleteThongBao(Guid MaThongBao)
        {
            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoDelete]", DBParameterFactory.Create("MaThongBao", MaThongBao));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public ThongBaoDTO UpdateThongBao(ThongBaoDTO thongBaoDTO)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_ThongBaoUpdate]", GetParameterArray(thongBaoDTO));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToThongBaoDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #region Private Methods
        private System.Data.SqlClient.SqlParameter[] GetParameterArray(ThongBaoDTO thongBaoDTO)
        {
            SqlParameter[] parameters = new SqlParameter[10];
            parameters[0] = DBParameterFactory.Create("MaThongBao", thongBaoDTO.MaThongBao);
            parameters[1] = DBParameterFactory.Create("TieuDeThongBao", thongBaoDTO.TieuDeThongBao);
            parameters[2] = DBParameterFactory.Create("NoiDungThongBao", thongBaoDTO.NoiDungThongBao);
            parameters[3] = thongBaoDTO.NguoiGuiId != null ? DBParameterFactory.Create("NguoiGui", thongBaoDTO.NguoiGuiId) : DBParameterFactory.Create("NguoiGui",SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[4] = DBParameterFactory.Create("NgayGuiThongBao", thongBaoDTO.NgayGuiThongBao);
            parameters[5] = thongBaoDTO.MaTaiLieu1 != null ? DBParameterFactory.Create("MaTaiLieu1", thongBaoDTO.MaTaiLieu1) : DBParameterFactory.Create("MaTaiLieu1", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[6] = thongBaoDTO.MaTaiLieu2 != null ? DBParameterFactory.Create("MaTaiLieu2", thongBaoDTO.MaTaiLieu2) : DBParameterFactory.Create("MaTaiLieu2", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[7] = thongBaoDTO.MaTaiLieu3 != null ? DBParameterFactory.Create("MaTaiLieu3", thongBaoDTO.MaTaiLieu3) : DBParameterFactory.Create("MaTaiLieu3", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[8] = thongBaoDTO.MaTaiLieu4 != null ? DBParameterFactory.Create("MaTaiLieu4", thongBaoDTO.MaTaiLieu4) : DBParameterFactory.Create("MaTaiLieu4", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[9] = thongBaoDTO.MaTaiLieu5 != null ? DBParameterFactory.Create("MaTaiLieu5", thongBaoDTO.MaTaiLieu5) : DBParameterFactory.Create("MaTaiLieu5", SqlDbType.UniqueIdentifier, DBNull.Value);
            return parameters;
        }
        #endregion
    }
}
